package com.servlet;

import com.dao.imp.*;
import com.entity.Good;
import com.entity.Order;
import com.entity.ShoppingCart;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/28 16:29
 **/

@WebServlet(name = "CreatOrderFromSingleItemServlet", value = "/CreatOrderFromSingleItemServlet")
public class CreatOrderFromSingleItemServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("userId").toString());

        int address_id = Integer.parseInt(request.getParameter("shippingId"));
        int good_id = Integer.parseInt(request.getParameter("productId"));
        int count = Integer.parseInt(request.getParameter("count"));

        String addressSql = "SELECT * FROM address WHERE address_id="+address_id;
        JSONArray addressJson = JSONArray.fromObject(FindAddress.getAddress(addressSql));


        String goodSql = "SELECT * FROM goods WHERE good_id="+good_id;
        FindGoodsDaoImp findGoodsDaoImp = new FindGoodsDaoImp();
        Map<String, Good> goodMap = new HashMap<>();
//        System.out.println(findGoodsDaoImp.getGood(goodSql).getGood_id());
        goodMap.put(String.valueOf(good_id),findGoodsDaoImp.getGood(goodSql));
        JSONObject goodJson = JSONObject.fromObject(goodMap);

        JSONObject amountJson = JSONObject.fromObject("{\'"+good_id+"\':"+count+"}");


        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String date = formatter.format(new Date(System.currentTimeMillis()));

        String insertSql = "INSERT INTO orders (user_id, order_creatTime, good_amount, receive_address, good, status, isPay) VALUES (" +
                user_id+", \'" +
                date+"\', \'" +
                amountJson +"\', \'" +
                addressJson.get(0)+"\', \'" +
                goodJson+"\', " +
                "DEFAULT,DEFAULT)";

        AddUpdDelDaoImp addUpdDelDaoImp = new AddUpdDelDaoImp();
        addUpdDelDaoImp.addUpdDelGoods(insertSql);


        String orderSql = "SELECT * FROM orders WHERE user_id="+user_id+" AND order_creatTime=\'"+date+"\' AND JSON_CONTAINS(good_amount,JSON_OBJECT('"+good_id+"', "+count+"))";
//        System.out.println(orderSql);
        Order order = FindOrder.getOrderInfo(orderSql);


        JSONObject json = JSONObject.fromObject("{order_id:"+order.getOrder_id()+"}");
        response.getWriter().print(json);
    }
}
